package cn.glenn.eguser1.controller;

import cn.glenn.eguser1.base.response.JsonResult;
import cn.glenn.eguser1.mapper.UserMapper;
import cn.glenn.eguser1.pojo.dto.UserAddParam;
import cn.glenn.eguser1.pojo.dto.UserUpdateParam;
import cn.glenn.eguser1.pojo.enity.User;
import cn.glenn.eguser1.pojo.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("add")
    public JsonResult addUserInfo(UserAddParam userAddParam){
        User user = new User();
        BeanUtils.copyProperties(userAddParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int num = userMapper.addUser(user);
        if(num > 0){return JsonResult.success();}
        else return JsonResult.fail();
    }
    @GetMapping("list")
    public JsonResult userInfoList(){
        if(userMapper.userList()!=null){
            return JsonResult.success(userMapper.userList());
        }else return JsonResult.fail();
    }

    @PostMapping("del")
    public JsonResult deleteUserById(Long id){
        int num = userMapper.deleteUserById(id);
        if(num > 0){return JsonResult.success();}
        else return JsonResult.fail();
    }

    @PostMapping("update")
    public JsonResult updateUserInfo(UserUpdateParam userUpdateParam){
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        user.setUpdateTime(new Date());
        int num = userMapper.updateUser(user);
        if(num > 0){return JsonResult.success();}
        else return JsonResult.fail();
    }
}
